package com.zimi.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.bean.Constants;
import com.zimi.model.SysOrgCate;
import com.zimi.service.SysOrgCateService;
import com.zimi.util.GsonUtil;
import com.zimi.util.ShiroUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织分类表 api
 *
 * @author lnintr
 * @since 2019-05-24
 */
@RestController
@RequestMapping("sys-org-cate")
public class SysOrgCateApi {
    @Autowired
    private SysOrgCateService service;

    @PostMapping("list")
    @ApiOperation("查询机构分类信息")
    public Object getList(SysOrgCate sysOrgCate, int limit, int page) {
        Map<String, Object> objectMap = new HashMap<>();
        PageHelper.startPage(page, limit);
        List<SysOrgCate> list = service.getList(sysOrgCate);
        PageInfo<SysOrgCate> pageInfo = new PageInfo<>(list);
        objectMap.put("data", list);
        objectMap.put("count", pageInfo.getTotal());
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("getList")
    public Object getCateList(SysOrgCate cate) {
        Map map = new HashMap();
        List<SysOrgCate> list = service.getList(cate);
        map.put("code", 0);
        map.put("data", list);

        return map;
    }


    @PostMapping("allList")
    @ApiOperation("查询机构分类信息")
    public Object getAllList(SysOrgCate sysOrgCate) {

        Session session = ShiroUtils.getSession();
        boolean admin = (boolean) session.getAttribute(Constants.IS_ADMIN_FLAG);


        sysOrgCate.setSocState(1);
        Map<String, Object> objectMap = new HashMap<>();
        List<SysOrgCate> list = service.getList(sysOrgCate);
        objectMap.put("data", list);
        objectMap.put("isAdmin", admin);
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("add")
    @ApiOperation("添加机构分类")
    public Object add(SysOrgCate sysOrgCate) {
        service.insert(sysOrgCate);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("update")
    @ApiOperation("修改机构分类")
    public Object upd(SysOrgCate sysOrgCate) {
        service.update(sysOrgCate);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("delete")
    @ApiOperation("删除机构分类")
    public Object del(SysOrgCate sysOrgCate) {
        service.delete(sysOrgCate);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("move")
    @ApiOperation("移动机构类型顺序")
    public Object up(HttpServletRequest request) {
        String[] ma = request.getParameterValues("cateList");

        List<SysOrgCate> sysOrgCateList = GsonUtil.json2Objs(ma[0], SysOrgCate.class);
        Map map = new HashMap();
        map.put("code", service.move(sysOrgCateList));
        return map;
    }

    @PostMapping("recycle")
    @ApiOperation("恢复机构类型")
    public Object del(HttpServletRequest request) {
        String[] ma = request.getParameterValues("cateList");

        List<SysOrgCate> sysOrgCateList = GsonUtil.json2Objs(ma[0], SysOrgCate.class);
        Map map = new HashMap();
        map.put("code", service.recycle(sysOrgCateList));
        return map;
    }

    @PostMapping("getDateByName")
    @ApiOperation("查询机构分类")
    public Object getDateByName(SysOrgCate orgCate, int limit, int page) {
        PageHelper.startPage(page, limit);
        List<SysOrgCate> sysOrgCateList = service.getDateByName(orgCate);
        PageInfo<SysOrgCate> pageInfo = new PageInfo<>(sysOrgCateList);

        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", sysOrgCateList);
        map.put("count", pageInfo.getTotal());
        return map;
    }
}
